CREATE TABLE [dbo].[TMDOCU_PRRE]
(
[CO_EMPR] [dbo].[TD_VC_002] NOT NULL,
[CO_PROV] [dbo].[TD_VC_020] NOT NULL,
[CO_TIPO_DOCU] [dbo].[TD_VC_003] NOT NULL,
[NU_DOCU_PROV] [dbo].[TD_VC_020] NOT NULL,
[FE_EMIS] [dbo].[TD_DT_001] NOT NULL,
[CO_COND_PAGO] [dbo].[TD_VC_003] NOT NULL,
[CO_UNID_CONC] [dbo].[TD_VC_003] NOT NULL,
[FE_VENC] [dbo].[TD_DT_001] NOT NULL,
[FA_TIPO_CAMB] [dbo].[TD_NU_015_006] NOT NULL CONSTRAINT [DF__TMDOCU_PR__FA_TI__70B3A6A6] DEFAULT ((1.000)),
[CO_MONE] [dbo].[TD_VC_003] NOT NULL,
[IM_BRUT_AFEC] [dbo].[TD_NU_016_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__IM_BR__71A7CADF] DEFAULT ((0)),
[FE_PROG_PAGO] [dbo].[TD_DT_001] NULL,
[IM_BRUT_INAF] [dbo].[TD_NU_016_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__IM_BR__729BEF18] DEFAULT ((0)),
[FE_REGI_COMP] [dbo].[TD_DT_001] NULL,
[PC_IMP1] [dbo].[TD_NU_009_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__PC_IM__73901351] DEFAULT ((0)),
[IM_IMP1] [dbo].[TD_NU_016_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__IM_IM__7484378A] DEFAULT ((0)),
[PC_IMP2] [dbo].[TD_NU_009_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__PC_IM__75785BC3] DEFAULT ((0)),
[CO_IMP1] [dbo].[TD_VC_003] NULL,
[IM_IMP2] [dbo].[TD_NU_016_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__IM_IM__766C7FFC] DEFAULT ((0)),
[PC_IMP3] [dbo].[TD_NU_009_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__PC_IM__7760A435] DEFAULT ((0)),
[CO_IMP2] [dbo].[TD_VC_003] NULL,
[IM_IMP3] [dbo].[TD_NU_016_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__IM_IM__7854C86E] DEFAULT ((0)),
[IM_TOTA] [dbo].[TD_NU_016_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__IM_TO__7948ECA7] DEFAULT ((0)),
[IM_PAGA] [dbo].[TD_NU_016_004] NOT NULL CONSTRAINT [DF__TMDOCU_PR__IM_PA__7A3D10E0] DEFAULT ((0)),
[CO_IMP3] [dbo].[TD_VC_003] NULL,
[TI_DOCU_ORIG] [dbo].[TD_VC_003] NULL,
[NU_DOCU_ORIG] [dbo].[TD_VC_020] NULL,
[FE_DOCU_ORIG] [dbo].[TD_DT_001] NULL,
[DE_OBSE] [dbo].[TD_VC_100] NULL,
[TI_SITU] [dbo].[TD_VC_003] NOT NULL,
[CO_ENTI_APRO] [dbo].[TD_VC_020] NULL,
[TI_ENTI_APRO] [dbo].[TD_VC_001] NULL,
[ST_APRO] [dbo].[TD_VC_001] NULL,
[NU_CORR_PROV] [dbo].[TD_VC_020] NULL,
[CO_ESTA_DOCU] [dbo].[TD_VC_003] NULL,
[CO_ENTI_GENE] [dbo].[TD_VC_020] NOT NULL,
[TI_ENTI_GENE] [dbo].[TD_VC_001] NOT NULL,
[FE_CTRL] [dbo].[TD_DT_001] NULL,
[NU_CORR_COMP] [dbo].[TD_VC_020] NULL,
[DE_GLOS] [dbo].[TD_VC_200] NULL,
[CO_USUA_CREA] [dbo].[TD_VC_008_USUA] NOT NULL CONSTRAINT [DF__TMDOCU_PR__CO_US__7B313519] DEFAULT (suser_sname()),
[FE_USUA_CREA] [dbo].[TD_DT_001_SIST] NOT NULL CONSTRAINT [DF__TMDOCU_PR__FE_US__7C255952] DEFAULT (getdate()),
[CO_USUA_MODI] [dbo].[TD_VC_008_USUA] NOT NULL CONSTRAINT [DF__TMDOCU_PR__CO_US__7D197D8B] DEFAULT (suser_sname()),
[FE_USUA_MODI] [dbo].[TD_DT_001_SIST] NOT NULL CONSTRAINT [DF__TMDOCU_PR__FE_US__7E0DA1C4] DEFAULT (getdate())
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE Trigger [dbo].[TI_TMDOCU_PRRE_I01] on [dbo].[TMDOCU_PRRE] for INSERT
/*---------------------------------------------------------*/
/*---- Empresa              : OFISIS S.A.              ----*/
/*---- Cliente              : OFISIS S.A.              ----*/
/*---- Sistema              : OFITESO                  ----*/
/*---- M½dulo               : Empresa                  ----*/
/*---- Programa             : Trigger                  ----*/
/*---- Script               : TRIGGERS_TMDOCU_PRRE.SQL ----*/
/*---- Nombre TR            : TI_TMDOCU_PRRE_I01       ----*/
/*---- Desarrollado por     : Daniel Chiang  ----*/
/*---- Fecha Creaci½n       : 15/06/2001               ----*/
/*---- Base de Datos        : SQL_Server               ----*/
/*---- Versi½n              : 7.0                      ----*/
/*---- Invoca a SP          :                          ----*/
/*---------------------------------------------------------*/
/*---- Modificado 1 por     :                          ----*/
/*---- Fecha Modificaci½n   :                          ----*/    
/*---- Detalle Modificacicn :                          ----*/  
/*---------------------------------------------------------*/
/*---- Comentarios: Genera Numero Correlativo, para    ----*/
/*----              la secuencia de ingreso de Compra  ----*/
/*----                                                 ----*/
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
/*---- Drop TRIGGER tI_TMDOCU_PRRE_I01                 ----*/
/*---- Grant All On tI_TMDOCU_PRRE_I01 to PUBLIC       ----*/
/*---------------------------------------------------------*/
NOT FOR REPLICATION  
As
Begin
  Declare
  @VSNU_CORR_COMP  TD_VC_020,
  @VSST_REGI_COMP  TD_VC_020,
  @VNNU_CORR_COMP  Int,
  @CSCO_EMPR       TD_VC_002,
  @CSCO_PROV       TD_VC_020,
  @CSCO_TIPO_DOCU  TD_VC_003,
  @CSNU_DOCU_PROV  TD_VC_020,
  @CDFE_REGI_COMP  TD_DT_001
 
  Declare CT_TMDOCU_PRRE_I01 Cursor
  For
  Select CO_EMPR, CO_PROV, CO_TIPO_DOCU, NU_DOCU_PROV,
         FE_REGI_COMP
    From inserted
  
  Open  CT_TMDOCU_PRRE_I01
  Fetch CT_TMDOCU_PRRE_I01 Into
        @CSCO_EMPR, @CSCO_PROV, @CSCO_TIPO_DOCU, @CSNU_DOCU_PROV,
        @CDFE_REGI_COMP
  While @@Fetch_Status = 0
  Begin
    If @@Fetch_Status < 0
      Begin
        Close CT_TMDOCU_PRRE_I01
        Deallocate CT_TMDOCU_PRRE_I01
      End
 
      -- Verifica si Tipo de Documento aparece en registro de Compras --
      Select @VSST_REGI_COMP = Isnull( ST_REGI_COMP, 'N')
      From TTDOCU_CNTB
      Where TI_DOCU = @CSCO_TIPO_DOCU
 
      Select @VNNU_CORR_COMP = 0
 
      If @VSST_REGI_COMP = 'S'
         Begin
 
          Select @VNNU_CORR_COMP = Isnull( NU_CORR, 0 ) + 1
          From   TTCORR_DOPR
          Where  CO_EMPR = @CSCO_EMPR
          And    NU_MESE  = Month( @CDFE_REGI_COMP )
          And    NU_ANNO  = Year( @CDFE_REGI_COMP )
 
          If @VNNU_CORR_COMP = 0
              Select @VNNU_CORR_COMP = 1
 
          If Not Exists (Select CO_EMPR    
                  From TTCORR_DOPR       
                  Where  CO_EMPR = @CSCO_EMPR
                  And    NU_MESE  = Month( @CDFE_REGI_COMP )
                  And    NU_ANNO  = Year( @CDFE_REGI_COMP ) )
 
            Insert Into TTCORR_DOPR(CO_EMPR, NU_MESE, NU_ANNO, NU_CORR )
            Values (@CSCO_EMPR, Month( @CDFE_REGI_COMP ), Year( @CDFE_REGI_COMP ), @VNNU_CORR_COMP )
         Else
              Update TTCORR_DOPR
              Set    NU_CORR = @VNNU_CORR_COMP 
              Where  CO_EMPR = @CSCO_EMPR
              And    NU_MESE  = Month( @CDFE_REGI_COMP )
              And    NU_ANNO  = Year( @CDFE_REGI_COMP )
 
              Select @VNNU_CORR_COMP = @VNNU_CORR_COMP + 1000000
              Select @VSNU_CORR_COMP = SubString( Convert(Varchar(07), @VNNU_CORR_COMP), 2, 6)
              Select @VSNU_CORR_COMP = SubString( Convert(Varchar(03), (Month( @CDFE_REGI_COMP ) + 100)), 2, 2) + @VSNU_CORR_COMP
              Select @VSNU_CORR_COMP = SubString( Convert(Varchar(04), (Year( @CDFE_REGI_COMP ) ) ), 3, 2) + @VSNU_CORR_COMP
 
              Update TMDOCU_PRRE
              Set    NU_CORR_COMP = @VSNU_CORR_COMP
              Where  CO_EMPR = @CSCO_EMPR
              And    CO_PROV = @CSCO_PROV
              And    CO_TIPO_DOCU = @CSCO_TIPO_DOCU
              And    NU_DOCU_PROV = @CSNU_DOCU_PROV
 
      End
 
      Fetch CT_TMDOCU_PRRE_I01 Into
            @CSCO_EMPR, @CSCO_PROV, @CSCO_TIPO_DOCU, @CSNU_DOCU_PROV,
            @CDFE_REGI_COMP
  End
  Close CT_TMDOCU_PRRE_I01
  Deallocate CT_TMDOCU_PRRE_I01
End
/*------------------- Fin de Trigger ----------------------*/
GO
ALTER TABLE [dbo].[TMDOCU_PRRE] ADD CONSTRAINT [PK__TMDOCU_P__3D80D1474CD638E3] PRIMARY KEY CLUSTERED  ([CO_EMPR], [CO_PROV], [CO_TIPO_DOCU], [NU_DOCU_PROV]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TMDOCU_PRRE] ADD CONSTRAINT [FK__TMDOCU_PR__CO_EM__0075D87A] FOREIGN KEY ([CO_EMPR]) REFERENCES [dbo].[TMPARA_TESO] ([CO_EMPR])
GO
